#!/usr/bin/env python

from os import listdir
from os.path import isfile, join
from facerecognition import recognize_faces
from imgutils import load_image

templates_path = './templates'
photos_path = './photos'


print 'Loading images...'
people = [ f for f in listdir(templates_path) if not isfile(templates_path+'/'+f) and not f.startswith('.') ]

# templates - list of pairs: (person name, list of images)
templates = [(person_name,[load_image(templates_path+'/'+person_name+'/'+f) for f in listdir(templates_path+'/'+person_name) if isfile(templates_path+'/'+person_name+'/'+f) ])for person_name in people]
 
# photos - list of pairs: (photo name, image)
photos = [ (f,load_image(photos_path+'/'+f)) for f in listdir(photos_path) if isfile(photos_path+'/'+f) and not f.startswith('.') ]

# result - dictionary: photo_name -> person_name
result = recognize_faces(templates,photos)

# analyse results, print the numbers
matches = 0
for photo_name, person_name in result.iteritems():
    if photo_name.startswith(person_name):
        matches += 1
print 'Finished. Accuracy: '+("%.2f" % (float(matches)*100.0/len(result)))+'% ('+str(matches)+'/'+str(len(result))+')'